import { Component, ChangeDetectorRef, ChangeDetectionStrategy } from "@angular/core";
import { CompanyService } from "../../company.service";
import { Subscription } from "rxjs";
import { CompanyEsgService } from "../companyEsg.service";
import { EsgOverallPerformanceSummary } from "../summary/models/EsgOverallPerformanceSummary.model";
import { CompanyBaseWidget } from "../../shared/companyBaseWidget/companyBase.widget";
import { ActivatedRoute, Router } from "@angular/router";

/**
 * ESG widget component
 * 
 * Author: Sorin Spiru
 * Date: 04.03.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: './widget.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class WidgetComponent extends CompanyBaseWidget {
    private companySub: Subscription;
    private esgSub: Subscription;
    public esgOverallPerformanceSummary: EsgOverallPerformanceSummary;

    constructor(
        private cdRef: ChangeDetectorRef,
        private route: ActivatedRoute,
        private router: Router,
        private companyService: CompanyService,
        private companyEsgService: CompanyEsgService) {
        super(route, router);
    }

    ngOnInit() {
        this.companySub = this.companyService.company.subscribe(company => {
            this.esgSub = this.companyEsgService.getEsgOverallPerformanceSummary(company.id).subscribe(data => {
                this.esgOverallPerformanceSummary = data;
                this.cdRef.markForCheck();
            });
        });
    }

    ngOnDestroy() {
        this.companySub.unsubscribe();
        this.esgSub.unsubscribe();
    }
}